<?php


namespace app\system\controller;

class Agent extends AdminBase
{
	public function getlist()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\AgentService::getlist($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function getinfo()
	{
		$options = $this->request->get();
		$result = \app\system\service\AgentService::getinfo($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function add()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["account" => "require", "name" => "require", "password" => "require", "qq" => "require", "disable" => "require", "add_subordinate_authority" => "require", "divide_subordinate_add_subordinate_authority" => "require"], ["account.require" => "账号 不能为空", "name.require" => "名称 不能为空", "password.require" => "密码 不能为空", "qq.require" => "qq 不能为空", "disable.require" => "是否禁用 不能为空", "add_subordinate_authority.require" => "子代权限 不能为空", "divide_subordinate_add_subordinate_authority.require" => "给子代设置子代权限 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AgentService::add($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require", "name" => "require", "qq" => "require", "disable" => "require", "add_subordinate_authority" => "require", "divide_subordinate_add_subordinate_authority" => "require"], ["id.require" => "id 不能为空", "name.require" => "名称 不能为空", "qq.require" => "qq 不能为空", "disable.require" => "状态 不能为空", "add_subordinate_authority.require" => "子代权限 不能为空", "divide_subordinate_add_subordinate_authority.require" => "给子代设置子代权限 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AgentService::edit($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "编辑成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require"], ["id.require" => "id 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AgentService::del($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_whole()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require"], ["id.require" => "id 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$options["mode"] = "del_whole";
		$result = \app\system\service\AgentService::del($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function revise_money()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require", "type" => "require", "number" => "require", "remarks" => "require"], ["id.require" => "id不能空！", "type.require" => "修改类型不能空！", "number.require" => "修改数量不能空！", "remarks.require" => "备注不能空！"]);
		if (!$validate->check($options)) {
			return json(["code" => -1, "msg" => $validate->getError()]);
		}
		$result = \app\system\service\AgentService::revise_money($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "修改成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_recharge_card_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\AgentService::get_recharge_cardlist($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function add_recharge_card()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["num" => "require", "type" => "require", "value" => "require"], ["num.require" => "num 不能为空", "type.require" => "type. 不能为空", "value.require" => "value 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AgentService::add_recharge_card($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_recharge_card()
	{
		$options = $this->request->post();
		$result = \app\system\service\AgentService::del_recharge_card($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_app_binding_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\AgentService::get_app_binding_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_app_binding_info()
	{
		$options = $this->request->get();
		$result = \app\system\service\AgentService::get_app_binding_info($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit_app_agent_binding()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "id" => "require", "authority_config" => "require|array"], ["appid.require" => "appid 不能为空", "id.require" => "id. 不能为空", "authority_config.require" => "config 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AgentService::edit_app_agent_binding($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "编辑成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function add_app_binding()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "id" => "require"], ["appid.require" => "appid 不能为空", "id.require" => "代理应用绑定ID 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AgentService::add_app_binding($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_app_binding()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "id" => "require"], ["appid.require" => "appid 不能为空", "id.require" => "代理应用绑定ID 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AgentService::del_app_binding($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_buy_order_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$options["appid"] = 0;
		$result = \app\system\service\AppService::get_buy_order_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_binding_app_data()
	{
		$options = $this->request->get();
		$validate = \think\Validate::make(["appid" => "require", "id" => "require"], ["appid.require" => "appid 不能为空", "id.require" => "代理应用绑定ID 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AgentService::get_binding_app_data($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function subordinate_move()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["oid" => "require", "id" => "require"], ["oid.require" => "迁移目标ID 不能为空", "id.require" => "迁移对象ID 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AgentService::subordinate_move($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "迁移成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function binding_app_card_disable()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "id" => "require"], ["appid.require" => "appid 不能为空", "id.require" => "代理应用绑定ID 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AgentService::binding_app_card_disable($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "设置成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
}